var api = require('./../../config/api.js');
var app = getApp();
var util = require('./../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    more: "为保证服务质量,2级以上骑士，账户需保留一定保证金.对于未完任务和违反骑士等级规则等行为进行扣钱处罚（如有意外情况，可以向平台申述）。当金额为负，则将取消骑士资格。保证无需额外支付，是由完成订单获得的钱，部分金额暂时无法提现。骑士每完成一单，会积累信用值。当骑士达到一定的好评率或者信用值提高时，保证金将越来越少，最终无需保证金，当违规时以扣除信用值代替。",
    rule:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    // 获取报名信息
    util.myRequest(api.ApplyInfo, false, function(data){
      var rule = that.infoFormat(data.data);
      that.setData({ rule: rule });
    });
    
  },

  infoFormat: function(data){
    var returnData = new Array();
    for (var i = 0; i < data.length; i++){
      var obj = {};
      obj.id = data[i].klId;
      obj.level = data[i].klLevel;
      obj.name = data[i].klName;
      obj.desc = data[i].klDesc;
      obj.brokerage = data[i].klFeePercent;
      obj.pledge = data[i].klPledge;
      obj.permission = JSON.parse(data[i].klRule);
      returnData.push(obj);
    }
    return returnData;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.globalData.shareInfo;
  },

  /**
   * 用户点击右上角分享
   */
  explain: function (e) {
    this.showExplain(e.currentTarget.id);
  },

  showExplain: function (index){
    var that = this;
    wx.showModal({
      title: '说明',
      content: that.data.rule[index].desc,
      showCancel: false,
      success: function (res) {
        var rule = that.data.rule;
        rule[index].isShow = true;
        that.setData({ rule: rule});
      }
    })
  },

  // 申请按钮
  apply: function (e){
    var that = this;

    wx.redirectTo({
      url: './../applyDetail/applyDetail?level=' + that.data.rule[e.currentTarget.id].level + '&id=' + that.data.rule[e.currentTarget.id].id
    })
    return;
    if (!that.data.rule[e.currentTarget.id].isShow){
      that.showExplain(e.currentTarget.id);
      return;
    }

    wx.showModal({
      title: '确定要申请为' + that.data.rule[e.currentTarget.id].level +'级骑士吗',
      content: '如果觉得不合适，也可以再申请为其它等级',
      success: function (res) {
        if (res.confirm) {
          that.submitApply(that.data.rule[e.currentTarget.id].id);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 提交申请
  submitApply: function(klId){

    var data = {
      token: app.globalData.userInfo.token,
      klId: klId,
    }

    util.myRequest(api.Apply, data, function(data){
      if (data !== false){
        wx.redirectTo({
          url: './../wait/wait?id=' + klId
        })
      }
    });
  },

  // 跳转时间选择
  goSchedule: function(klId){
    wx.navigateTo({
      url: './../schedule/schedule?klId=' + klId
    })
  },

})